<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\ActivatedCode;
use support\Db;
use support\Request;
use support\Response;
use plugin\admin\app\model\Product;
use support\exception\BusinessException;

/**
 * 产品
 */
class ProductController extends Crud
{

    /**
     * @var Product
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Product;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('product/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('product/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }

        return view('product/update');
    }

    /**
     * 删除商品时候直接将本地商品图也一并删除
     * @param Request $request
     * @return Response
     */
    public function delete_image(Request $request): Response
    {
        $data = $request->post();
        print_r($data['pushDeleteImg']);
        $isDelete1 = false;
//        $isDelete2 = false;
//        $isDelete3 = false;
//        $isDelete4 = false;
        for ($i = 0; $i < count($data['pushDeleteImg']); $i++) {
            $bean = $data['pushDeleteImg'][$i];
            if (empty($bean['product_img'])){
                continue;
            }
            $deleteImages = str_replace('/app/admin/', '', $bean['product_img']);
            $deleteImages = str_replace('.lg.png', '', $deleteImages);
//            print_r("删除链接：" . $deleteImages);
//            print_r("");
//            print_r("删除链接完整路径：" . base_path() . "/plugin/admin/public/" . $deleteImages . ".lg.png");
//            print_r("");
//            print_r("删除链接完整路径：" . base_path() . "/plugin/admin/public/" . $deleteImages . ".md.png");
//            print_r("");
//            print_r("删除链接完整路径：" . base_path() . "/plugin/admin/public/" . $deleteImages . ".png");
//            print_r("");
//            print_r("删除链接完整路径：" . base_path() . "/plugin/admin/public/" . $deleteImages . "sm.png");
//            print_r("");
            $isDelete1 = unlink(base_path() . "/plugin/admin/public/" . $deleteImages . ".lg.png");
//            $isDelete2 = unlink(base_path() . "/plugin/admin/public/" . $deleteImages . ".md.png");
//            $isDelete3 = unlink(base_path() . "/plugin/admin/public/" . $deleteImages . ".png");
//            $isDelete4 = unlink(base_path() . "/plugin/admin/public/" . $deleteImages . ".sm.png");
        }
        return $this->json(0, 'ok', ['data'=>$isDelete1==true]);
    }

    public function query_product_type(Request $request){
        $product_types = Db::table('wa_product_type')->get();
        if (empty($product_types)){
            return json(1, '请预先配置产品类型');
        }
        return json(['code' => 0, 'msg' => 'success','data'=>$product_types]);
    }

    /**
     * 批量添加激活码
     * @param Request $request
     * @return Response
     */
    public function add_activated_code(Request $request):Response{
        if ($request->method()==='POST'){
            $product_id = $request->post('id');
            $product_name = $request->post('product_name');
            $product_type = $request->post('product_type');
            $product_activated_codes = $request->post('product_activated_code');
            if (str_ends_with($product_activated_codes, ";")){
                $product_activated_codes = $product_activated_codes.substr(0,strlen($product_activated_codes)-1);
            }
            $array = explode(";", $product_activated_codes);
            $isSaveOver = false;
            for ($i=0;$i<count($array);$i++){
                $activated = new ActivatedCode();
                $activated->product_id =$product_id;
                $activated->product_name =$product_name;
                $activated->product_type =$product_type;
                $activated->product_activated_code =$array[$i];
                $activated->product_activated_code_is_used =0;//0 未使用 ；1 已使用
                $actOneData = Db::table('wa_activated_code')->where('product_activated_code',$array[$i])->first();
                if (empty($actOneData)){
                    $isSaveOver = $activated->save();
                }else{
                    $isSaveOver = false;
                    return $this->json(1, '激活码 '.$array[$i].' 异常,请检查并删除');
                }
            }
            if (!$isSaveOver){
                return $this->json(1, 'error');
            }
            return $this->json(0, 'ok', ['result' => $isSaveOver]);
        }
        return view('product/add_activated_code');
    }
}
